package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.DrawingPrintHstDto;
import com.be.beadmin.product.dto.input.DrawingPrintHstDtoByWoIdAndPage;
import com.be.beadmin.product.dto.output.DrawingPrintHstEx;
import com.be.beadmin.product.service.DrawingPrintHstService;
import com.be.beadmin.project.Util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-04-03
**/
@Api(tags = {"图纸打印管理"})
@RestController
public class DrawingPrintHstController {

    @Autowired
    private  DrawingPrintHstService drawingPrintHstService;


    MinioUtil minioUtil = new MinioUtil();

    @ApiOperation("获取图纸打印记录分页信息")
    @PostMapping(value = "/product/getDrawingPrintHstByPage")
    public ResultModel getDrawingPrintHstByPage(@RequestBody PageEntity pageEntity) throws Exception {
        return ResultModel.success(drawingPrintHstService.getDrawingPrintHstByPage(pageEntity)) ;
    }

    @ApiOperation("（根据工单id）图纸打印分页信息查询")
    @PostMapping(value = "/product/getDrawingPrintHstByWoIdAndPage")
    public ResultModel getDrawingPrintHstByWoIdAndPage(@RequestBody DrawingPrintHstDtoByWoIdAndPage drawingPrintHstDtoByWoIdAndPage) throws Exception {

        try {
            return ResultModel.success(drawingPrintHstService.getDrawingPrintHstByWoIdAndPage(drawingPrintHstDtoByWoIdAndPage)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }

    }

    @ApiOperation("单独新增的额外接口,")
    @PostMapping("/product/addDrawingPrintHst2")
    public ResultModel addDrawingPrintHst2(@RequestBody DrawingPrintHstDto drawingPrintHstDto){
        drawingPrintHstService.addDrawingPrintHst2(drawingPrintHstDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("图纸打印时自动添加打印记录）")
    @PostMapping("/product/addDrawingPrintHst")
    public ResultModel addDrawingPrintHst(@RequestBody DrawingPrintHstDto drawingPrintHstDto){
        drawingPrintHstService.addDrawingPrintHst(drawingPrintHstDto);
        return ResultModel.success(ResultModel.ADD);
    }

//    @PostMapping("/product/delDrawingPrintHst")
//    public ResultModel delDrawingPrintHst(@RequestBody DrawingPrintHstDto drawingPrintHstDto){
//        drawingPrintHstService.delDrawingPrintHst(drawingPrintHstDto.getId());
//        return ResultModel.success(ResultModel.DELETE);
//    }


//    @PostMapping("/product/updateDrawingPrintHst")
//    public ResultModel updateDrawingPrintHst(@RequestBody DrawingPrintHstDto drawingPrintHstDto){
//        drawingPrintHstService.updateDrawingPrintHst(drawingPrintHstDto);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

    @ApiOperation("导出")
    @PostMapping("/product/exportDrawingPrintHst")
    public void exportDrawingPrintHst(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<DrawingPrintHstEx> drawingPrintHstPage = drawingPrintHstService.getDrawingPrintHstByPage(pageEntity);
        List<DrawingPrintHstEx> drawingPrintHstExes = drawingPrintHstPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("DrawingPrintHst接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DrawingPrintHstEx.class).sheet("sheet1").doWrite(drawingPrintHstExes);
    }
}